#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define DATA_LEN 1024
unsigned char data[DATA_LEN];

int main(int argc, char* argv[])
{
    // 和驱动一样的数据
    int i;
    for (i = 0;i < sizeof(data);i++) {
        data[i] = i % 0xff;
    }

    // 1.打开 字符设备
    char devname[] = "/dev/chardev";
    int fd;
    fd = open(devname, O_RDWR);
    if (fd <= 0) {
        perror("open dev failed");
        return 1;
    }

    // 3.流式读取数据
    int length = 64;
    int off = 0;
    int ret = 0;
    unsigned char buf[DATA_LEN];
    while (length >= 64 || off < DATA_LEN) {
        length = read(fd, buf + off, length);
        off += length;
    }
    printf("off=%d\n",off);

    // 4.比较是否相等
    ret = memcmp(data, buf, DATA_LEN);
    if (ret == 0) {
        printf("data, buff 内存相同, ret=%d\n", ret);
    }
    else {
        printf("--> data, buff 内存不相同, ret=%d\n", ret);
    }

    return 0;
}
/* 运行结果:
data, buff 内存相同, ret=0

 */